import { Inject, Injectable } from '@nestjs/common';
import { CreateHomeMediaDto } from './dto/create-home-media.dto';
import { UpdateHomeMediaDto } from './dto/update-home-media.dto';
import { PrismaService } from 'src/prisma.service';
import { ReturnHomeMediaDto } from './dto/return-home-media.dto';

@Injectable()
export class HomeMediaService {
  @Inject()
  prismaService: PrismaService;

  async create(
    createHomeMediaDto: CreateHomeMediaDto,
  ): Promise<ReturnHomeMediaDto> {
    return await this.prismaService.homeMedia.create({
      data: {
        fileId: createHomeMediaDto.fileId,
        remark: createHomeMediaDto.remark,
        order: createHomeMediaDto.order,
        isDisplayed: createHomeMediaDto.isDisplayed,
      },
      include: {
        file: true,
      },
    });
  }

  async findAll(): Promise<ReturnHomeMediaDto[]> {
    return await this.prismaService.homeMedia.findMany({
      where: {
        isDeleted: false,
      },
      orderBy: {
        order: 'asc',
      },
      include: {
        file: true,
      },
    });
  }

  async findOne(id: number): Promise<ReturnHomeMediaDto | null> {
    return await this.prismaService.homeMedia.findFirst({
      where: {
        id,
        isDeleted: false,
      },
      include: {
        file: true,
      },
    });
  }

  async update(
    id: number,
    updateHomeMediaDto: UpdateHomeMediaDto,
  ): Promise<ReturnHomeMediaDto> {
    return await this.prismaService.homeMedia.update({
      where: {
        id,
      },
      data: updateHomeMediaDto,
      include: {
        file: true,
      },
    });
  }

  async remove(id: number): Promise<ReturnHomeMediaDto> {
    return await this.prismaService.homeMedia.update({
      where: {
        id,
      },
      data: {
        isDeleted: true,
      },
      include: {
        file: true,
      },
    });
  }
}
